package com.fx.controller;

import com.alibaba.fastjson.JSONObject;
import com.fx.entity.OrderInfo;
import com.fx.entity.OrderItem;
import com.fx.service.OrderInfoService;
import com.fx.service.OrderItemService;
import com.fx.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 
 *
 * @author yfl
 * @email 617823298@qq.com
 * @date 2018-06-19 16:25:21
 */

@Controller
@RequestMapping("/orderInfo")
public class OrderInfoController extends BaseController {
    @Autowired
    private OrderInfoService orderInfoService;
    @Autowired
    private OrderItemService orderItemService;
    private static String pageTitle = "订单管理";
    private static String pageUrl = "/orderInfo";

    @GetMapping()
    String orderInfo(Model model) {
        model.addAttribute("pageInfo", PageInfo.getInstance(pageUrl, pageTitle, Constant.LIST_TITLE));
        return "orderInfo/orderInfo";
    }

    @ResponseBody
    @GetMapping("/list")
    public JSONObject list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        List<OrderInfo> orderInfoList = orderInfoService.getList(query);
        int total = orderInfoService.getCount(query);
        return renderPage(total, orderInfoList);
    }

    @GetMapping("/add")
    String add(Model model) {
        model.addAttribute("pageInfo", PageInfo.getInstance(pageUrl, pageTitle, Constant.ADD_TITLE));
        return "orderInfo/add";
    }

    @GetMapping("/edit/{flId}")
    String edit(@PathVariable("flId") String flId, Model model) {
        OrderInfo orderInfo =orderInfoService.getById(flId);
        model.addAttribute("pageInfo", PageInfo.getInstance(pageUrl, pageTitle, Constant.EDIT_TITLE));
        model.addAttribute("orderInfo", orderInfo);
        return "orderInfo/edit";
    }

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public R saveOrUpdate(OrderInfo orderInfo) {
        String id = orderInfo.getFlId();
        int result = 0;
        if (id == null || "".equals(id)) {
            result = orderInfoService.save(orderInfo);
        } else {
            result = orderInfoService.updateById(orderInfo);
        }
        if (result != 0) {
            return R.ok("操作成功!");
        }
        return R.error("操作失败!");
    }

    /**
     * 批量删除
     */
    @PostMapping("/batchDel")
    @ResponseBody
    public R remove(@RequestParam("ids") String[] ids) {
        if (orderInfoService.batchRemove(ids) != 0) {
            return R.ok("操作成功!");
        }
        return R.error("操作失败!");
    }

    /**
     * 方法名称：我的订单接口
     * 概要说明：根据状态查询订单
     * @Ather EDZ
     * @Date 2018-6-25 14:19
     * @Param userId要获取的用户Id
     * @Param status 订单状态 0:待付款,1:待发货,2:待收货,3待评价,4退款中,5退款失败,6:退款成功,-1:订单结束
     * @Return
     */
    @GetMapping("/api/getOrderByStatus/{status}")
    @ResponseBody
    public R getOrderByStatus(HttpServletRequest request,@PathVariable("status") Integer status){
        String userId = String.valueOf(request.getAttribute("flId"));
        Map<String, Object> params = new HashMap<String,Object>();
        if(status != -1){
            params.put("flStatus",status);
        }
        params.put("flUserId",userId);
        List<OrderInfo> orderInfoList = orderInfoService.getList(params);
        for (OrderInfo orderInfo : orderInfoList){
            List<OrderItem> orderItemList = orderItemService.getOrderItemByOrderId(orderInfo.getFlId());
            orderInfo.setOrderItemList(orderItemList);
        }
        return R.ok(orderInfoList);
    }

    /**
     * 方法名称：创建订单
     * 概要说明：创建订单api接口
     * @Ather EDZ
     * @Date 2018-6-26 9:11
     * @Param
     * @Return
     */
    @PostMapping("/api/generalOrder")
    @ResponseBody
    public R generalOrder(HttpServletRequest request,String addressId) {
        String userId = String.valueOf(request.getAttribute("flId"));
        return R.ok(orderInfoService.generalOrder(userId,addressId));
    }

    /**
     * 方法名称：H5微信支付
     * 概要说明：H5微信支付
     * @Ather EDZ
     * @Date 2018-6-26 10:00
     * @Param
     * @Return
     */
    @PostMapping("/api/orderPayment")
    @ResponseBody
    public R orderPayment(String orderId, HttpServletRequest request) throws Exception {
        String clientIp = request.getRemoteAddr();
        String serverUrl = UrlUtil.getServerUrl(request);
        Map<String,String> wxJsPay = orderInfoService.orderPayment(orderId,clientIp,serverUrl);
        return R.ok(wxJsPay);
    }

    /**
     * 方法名称：微信退款
     * 概要说明：微信退款
     * @Ather EDZ
     * @Date 2018-6-26 10:00
     * @Param
     * @Return
     */
    @PostMapping("/api/refund")
    @ResponseBody
    public R refund(String orderId) throws Exception {
        return R.ok(orderInfoService.refund(orderId));
    }

}
